<?php

namespace App\Http\Controllers\Admin;

use App\admin\Adlog;
use App\admin\Scrollimg;
use App\Http\Controllers\Admin\LogHander;
use App\Http\Controllers\Controller;
use App\Http\Middleware;
use Illuminate\Http\Request;
use Intervention\Image\ImageManagerStatic as Image;
use Illuminate\Routing\Controller as BaseController;

class ScrollController extends BaseController
{
	public function __construct() {
		$this->middleware('admindo');
	}
	public function index(Request $request)
	{
		$flight = Scrollimg::get();
		return view('admin.scrollindex',['data' => $flight]);
	}
	
	public function editeUp(Request $request){
		$id = $request->input('id');
		if($id == ""){
			return "找不到id";
		}
		$Scrollimg = new Scrollimg;
		$pathOne = "";$fileNameOne = "";
		
		if ($request -> hasFile('fileOne')) {
			$file = $request -> file('fileOne');
			$entension = $file -> getClientOriginalExtension();
			$ents = array("jpg", "jpge", "png", "bmp","gif");
			if(!in_array($entension, $ents)){
				return "只支持jpg, jpge, png, bmp,gif类型的图片";
			}
			$name = date("Ymd").str_random(10) . '.'.$entension;
			$path = public_path() .'/upload/scroll/'. $name;
			Image::make($file) -> save($path);
			$pathOne = 'upload/scroll/'. $name;
			$fileNameOne = $name;
		}else{
			return "请选择上传图片";
		}
		$temp = $Scrollimg::find($id);
		$temp->imgname = $fileNameOne;
		if($pathOne != ""){
			$temp->imgpath = $pathOne;
		}

		if($temp->save()){
			$cars=array('name'=>'editeUp','info'=>'图片上传','users'=>'滚动图片上传','cnt'=>$fileNameOne);
			$tempx = new LogHander;
			$tempx->logs($cars);
			return redirect('scrollindex');
		}else{
			return "更新失败";
		}
	}
}
